/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.yutong.system.controller;

import cn.yutong.framework.RestResponse;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

//import org.apache.shiro.SecurityUtils;

/**
 * 验证码生成
 *
 * @author lnexin@laiyun.com
 * @date 2017-05-05 23:10
 */
@RestController
@Api(tags= "验证码相关")
public class KaptchaController {

    @Autowired
    DefaultKaptcha kaptcha;

    @RequestMapping(value = "/kaptcha", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取一个新的验证码")
    public void defaultKaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        byte[] imageArrByte = null;
        ByteArrayOutputStream imageOS = new ByteArrayOutputStream();

        String verifyCode = kaptcha.createText();
        try {
            request.getSession().setAttribute("verifyCode", verifyCode);
            BufferedImage verifyImage = kaptcha.createImage(verifyCode);
            ImageIO.write(verifyImage, "jpg", imageOS);
        } catch (IOException e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            e.printStackTrace();
        }

        imageArrByte = imageOS.toByteArray();
        response.setDateHeader("Expires", 0);

        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");

        // return a jpeg
        response.setContentType("image/jpeg");

        ServletOutputStream responseOS = response.getOutputStream();
        responseOS.write(imageArrByte);
        responseOS.flush();
        responseOS.close();
    }

    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("验证验证码是否一致")
    public RestResponse verifyKaptcha(HttpServletRequest request) {
//        Object sessionCode = SecurityUtils.getSubject().getSession().getAttribute("verifyCode");
        Object sessionCode = request.getSession().getAttribute("verifyCode");
        if (sessionCode == null) return RestResponse.failure("验证码认证失败", "session信息错误,session中没有验证码");
        String verifyCode = sessionCode.toString();
        String frontCode = request.getParameter("frontCode");
        RestResponse response = RestResponse.newEntity();
        if (verifyCode.equals(frontCode)) {
            response.setRet(0);
            response.setMsg("验证通过");
        } else {
            response.setRet(-1);
            response.setMsg("验证码错误!");
        }
        response.setRet(0);
        return response;
    }
}
